package com.mybed.crm.controller;

import com.mybed.crm.anno.Log;
import com.mybed.crm.pojo.pojo.Menu;
import com.mybed.crm.pojo.pojo.User;
import com.mybed.crm.result.MenuResult;
import com.mybed.crm.result.Result;
import com.mybed.crm.result.ResultP;
import com.mybed.crm.service.MenuService;
import com.mybed.crm.service.UserService;
import com.mybed.crm.utils.thread.UserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/system/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private UserService userService;
    //todo 数据验证
    @GetMapping("/list")
    public Result list(){
        return Result.success(menuService.list());
    }
    @Log("新增菜单")
    @PostMapping
    public ResultP addSave(@RequestBody Menu menu){
        User user = userService.getById(UserThreadLocalUtil.getUser());
        menu.setCreateBy(user.getUserName());
        menu.setCreateTime(LocalDateTime.now());
        menuService.save(menu);
        return ResultP.success();
    }
    @Log("查询单个菜单")
    @GetMapping("/{id}")
    public Result menuByOne(@PathVariable Integer id){
        return Result.success(menuService.getById(id));
    }
    @Log("修改菜单")
    @PutMapping("修改菜单")
    public ResultP updateMenu(@RequestBody Menu menu){
        menuService.updateById(menu);
        return ResultP.success();
    }
    @Log("删除菜单")
    @DeleteMapping("/{id}")
    public ResultP deleteMenu(@PathVariable String id){
        menuService.removeById(id);
        return ResultP.success();
    }
    @GetMapping("/treeselect")
    public Result treeselect(){
        return menuService.treeselect();
    }

    @GetMapping("/roleMenuTreeselect/{id}")
    public MenuResult roleMenuTreeselect(@PathVariable Integer id){
        return menuService.roleMenuTreeselect(id);
    }

}
